package com.wontube.smr.dao.bo;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.wontube.core.dao.mybatis.QueryExample;
import com.wontube.smr.dao.mapper.web.HomeBannerMapper;
import com.wontube.smr.pub.DefaultDBHelper;
import com.wontube.smr.web.client.model.po.HomeBanner;

/**
 * home banner dao
 * 
 * @author hewc@wondershare.cn
 * 
 */
public final class HomeBannerSelectDao {
	private HomeBannerSelectDao() {
	}

	public static boolean saveOrUpdateHomeBanner(HomeBanner homeBanner) {
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			HomeBannerMapper mapper = session.getMapper(HomeBannerMapper.class);
			HomeBanner temp = mapper.selectByPrimaryKey(homeBanner.getId());
			int aff = 0;
			if (temp != null) {
				aff = mapper.updateByPrimaryKey(homeBanner);
			} else {
				aff = mapper.insert(homeBanner);
			}
			return aff == 1;
		} finally {
			session.close();
		}
	}

	public static boolean deleteHomeBanner(String pk) {
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			HomeBannerMapper mapper = session.getMapper(HomeBannerMapper.class);
			int aff = mapper.deleteByPrimaryKey(pk);
			return aff == 1;
		} catch (Exception e) {
			return false;
		} finally {
			session.close();
		}
	}

	public static List<HomeBanner> listHomeBanner() {
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		QueryExample ex = new QueryExample();
		ex.setOrderByClause("home_banner.sort_index");
		try {
			HomeBannerMapper mapper = session.getMapper(HomeBannerMapper.class);
			return mapper.selectByExample(ex);
		} finally {
			session.close();
		}
	}

	public static HomeBanner getHomeBannerByPK(String pk) {
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			HomeBannerMapper mapper = session.getMapper(HomeBannerMapper.class);
			return mapper.selectByPrimaryKey(pk);
		} finally {
			session.close();
		}
	}
}
